package com.zut.subject.domain.service.impl;
import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import com.zut.subject.common.enums.CategoryTypeEnum;
import com.zut.subject.common.enums.IsDeletedFlagEnum;
import com.zut.subject.domain.convert.ConvertCategory;
import com.zut.subject.domain.convert.ConvertLabel;
import com.zut.subject.domain.entity.SubjectCategoryBo;
import com.zut.subject.domain.entity.SubjectLabelBo;
import com.zut.subject.domain.entity.SubjectMappingBo;
import com.zut.subject.domain.service.SubjectLabelDomainService;
import com.zut.subject.infra.basic.entity.SubjectCategory;
import com.zut.subject.infra.basic.entity.SubjectLabel;
import com.zut.subject.infra.basic.entity.SubjectMapping;
import com.zut.subject.infra.basic.service.SubjectCategoryService;
import com.zut.subject.infra.basic.service.SubjectLabelService;
import com.zut.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.security.auth.Subject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {


    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public boolean add(SubjectLabelBo subjectLabelBo) {
        if(log.isInfoEnabled()){
            log.info("subjectLabelBoDomainService.add.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        subjectLabelBo.setIsDelete(0);
        int insert = subjectLabelService.insert(ConvertLabel.INSTANCE.toSubjectLabel(subjectLabelBo));
        if(log.isInfoEnabled()){
            log.info("subjectLabelBoDomainService.add.Infra:{}", ConvertLabel.INSTANCE.toSubjectLabel(subjectLabelBo));
        }

        return insert>0;

    }

    @Override
    public boolean update(SubjectLabelBo subjectLabelBo) {
        //bo转化为subjectLabel类型
        SubjectLabel subjectLabel = ConvertLabel.INSTANCE.toSubjectLabel(subjectLabelBo);
        if(log.isInfoEnabled()){
            log.info("subjectLabelBoDomainService.add.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        int update = subjectLabelService.update(subjectLabel);
        return update>0;
    }

    @Override
    public boolean delete(SubjectLabelBo subjectLabelBo) {
        SubjectLabel subjectLabel = ConvertLabel.INSTANCE.toSubjectLabel(subjectLabelBo);

        //设置isDelete为1，代表数据已经被删除
        subjectLabel.setIsDelete(IsDeletedFlagEnum.DELETED.getCode());
        if (log.isInfoEnabled()){
            log.info("domain层向infra层传输数据为：{}",subjectLabel);
        }
        int update = subjectLabelService.update(subjectLabel);
        //数据大于0返回true
        return update>0;
    }

    @Override
    public List<SubjectLabelBo> queryLabelByCategory(SubjectMappingBo subjectMappingBo) {
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectMappingBo.getCategoryId());

        //如果查询的分类为一级分类
        if (CategoryTypeEnum.PRIMARY.getCode()==subjectCategory.getCategoryType()){
//            SubjectLabelBo subjectLabelBo =new SubjectLabelBo();
//            subjectLabelBo.setCategoryId(subjectMappingBo.getCategoryId());
//            SubjectLabel subjectLabel1 = ConvertLabel.INSTANCE.toSubjectLabel(subjectLabelBo);
//            //分情况查询一级分类下的标签
//            List<SubjectLabel> subjectLabel =subjectLabelService.queryCondition(subjectLabel1);
//            List<SubjectLabelBo> bos = ConvertLabel.INSTANCE.toListSubjectLabelBo(subjectLabel);
//            return bos;
            return Collections.emptyList();


        }

        Integer categoryId = subjectMappingBo.getCategoryId();
        log.info("categoryId={}",categoryId);
        //注入分类id获得该分类下的标签集合
        List<SubjectMapping> list = subjectMappingService.queryLabelIdByCategoryId(categoryId);
        //将标签集合类转换为标签id集合
        List<Integer> collect = list.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        //通过查询标签id集合获得标签实体类集合
        List<SubjectLabel> LabelList = subjectLabelService.queryById(collect);
        List<SubjectLabelBo> bos = ConvertLabel.INSTANCE.toListSubjectLabelBo(LabelList);
        List<SubjectLabelBo> bos1 =new ArrayList<>();
        for (SubjectLabelBo label : bos) {
           label.setCategoryId(categoryId);
            bos1.add(label);
        }

        return bos1;

    }

}
